package com.itheima.vmmysql.Controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itheima.vmmysql.Service.CreateTableService;

@Controller
public class CreateTableController {

    @Autowired
    private CreateTableService createTableService;

    // 映射到 createtable.html 页面
    @GetMapping("/createtable")
    public String showCreateTablePage() {
        System.out.println("建表页面被访问");
        return "html/createtable"; // 返回视图名称，视图解析器会渲染 createtable.html
    }

    // 处理 AJAX 的建表请求
    @PostMapping("/createtable")
    @ResponseBody
    public Map<String, Object> createTable(@RequestBody Map<String, String> requestBody) {
        String sql = requestBody.get("sql");
        Map<String, Object> response = new HashMap<>();

        try {
            createTableService.createTable(sql);
            response.put("success", true);
        } catch (Exception e) {
            response.put("success", false);
            response.put("message", e.getMessage());
        }

        return response;
    }
}
